package com.htthorizon.model.service;

import com.htthorizon.excel.excel.ModelUseExcel;
import com.htthorizon.model.domain.FeedBackDto;
import com.htthorizon.model.domain.ModelUse;
import com.htthorizon.model.domain.ModelUseCityDto;
import com.htthorizon.model.enums.OperateType;
import com.htthorizon.model.mapper.ModelUseMapper;
import com.htthorizon.model.request.ModelUseSearch;
import com.htthorizon.model.response.ModelUseDto;
import com.htthorizon.system.api.RemoteDataDictService;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Brilliant
 */
@Service
public class ModelUseServiceImpl implements IModelUseService{

    @Autowired
    private ModelUseMapper modelUseMapper;


    @Override
    public List<ModelUse> list(ModelUseSearch modelUseSearch) {
        return modelUseMapper.list(modelUseSearch);
    }

    @Override
    public boolean save(ModelUse modelUse) {
        return modelUseMapper.insert(modelUse) > 0;
    }

    @Override
    public List<ModelUseDto> getModelUse(Integer specType,Integer page,Integer pageSize) {
        List<ModelUseDto> modelUse = modelUseMapper.getModelUse(specType, (page - 1) * pageSize, pageSize);
        HashSet<ModelUseDto> modelUseDtos = new HashSet<>(modelUse);
        return new ArrayList<>(modelUseDtos);
    }

    @Override
    public Map<String, Integer> getModelUseCity() {
       List<ModelUse> list= modelUseMapper.listAll();
        Map<String, List<ModelUse>> collect = list.stream().collect(Collectors.groupingBy((ModelUse::getCreateDept)));
        HashMap<String, Integer> map = new LinkedHashMap<>();

        List<Map.Entry<String, List<ModelUse>>> entries = new ArrayList<>(collect.entrySet());
        entries.sort(Map.Entry.comparingByValue(Comparator.comparing(List::size)));

// 以排序后的List<ModelUse>为值，重新构造一个Map<String, List<ModelUse>>集合
        Map<String, List<ModelUse>> sortedCollect = new LinkedHashMap<>();
        for (Map.Entry<String, List<ModelUse>> entry : entries) {
            sortedCollect.put(entry.getKey(), entry.getValue());
        }
        for (Map.Entry<String, List<ModelUse>> entry : sortedCollect.entrySet()) {
            map.put(entry.getKey(), entry.getValue().size());
            if (map.size()>=5){
                break;
            }
        }
        return map;
    }

    @Override
    public int modelException() {
        return modelUseMapper.modelException();
    }

    @Override
    public int modelBefore() {
        return modelUseMapper.modelBefore();
    }

    @Override
    public Integer getModelUseCount(Integer specType) {
        return modelUseMapper.getModelUseCount(specType);
    }

    @Override
    public List<ModelUseExcel> listAllDownload() {
        List<ModelUse> list = modelUseMapper.listAll();
        return list.stream().map(this::map2Excel).collect(Collectors.toList());
    }

    @Override
    public List<FeedBackDto> getFeedBack2() {
       return modelUseMapper.feedBack();
    }

    @Override
    public List<ModelUseCityDto> modelUseCityVo2() {
      return  modelUseMapper.modelUseCityVo2();
    }

    private ModelUseExcel map2Excel(ModelUse modelUse){
        ModelUseExcel modelUseExcel = new ModelUseExcel();
        BeanUtils.copyProperties(modelUse,modelUseExcel);

        if (modelUse.getType().equals(OperateType.QUERY.getValue()))
        {
            modelUseExcel.setType(OperateType.QUERY.getName());
        }else {
            modelUseExcel.setType(OperateType.DOWNLOAD.getName());
        }
        return modelUseExcel;
    }
}
